<template>
  <div class="app-container">
    <el-button type="primary" @click="openCreateWindow">发布套餐</el-button>

    <el-table :data="products" style="width: 100%;margin-top:30px;" border>
      <el-table-column align="center" label="#" width="50">
        <template v-slot="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="套餐名称" >
        <template v-slot="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="短信条数">
        <template v-slot="scope">
          {{ scope.row.msg_count }}条
        </template>
      </el-table-column>
      <el-table-column align="center" label="套餐价格" >
        <template v-slot="scope">
          {{ scope.row.price }}元
        </template>
      </el-table-column>
      <el-table-column align="center"    label="操作" width="200">
        <template v-slot="{row}">
          <el-button  size="mini"  type="danger"
                     @click="handleDelete(row)">
            下架
          </el-button>
          <el-button  size="mini"  type="primary"
                      @click="openUpdateWindow(row)">
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'修改套餐':'发布套餐'">
      <el-form ref="smsProductForm" :model="form.product" :rules="form.rules" label-width="80px" label-position="left">
        <el-form-item label="套餐名称">
          <el-input v-model="form.product.name"  />
        </el-form-item>
        <el-form-item label="套餐价格">
          <el-input v-model.number="form.product.price"  />
        </el-form-item>
        <el-form-item label="短信数量">
          <el-input v-model.number="form.product.msg_count"  />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="dialogType==='edit'?handleEdit():handleCreate()">发布</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {createSmsProducts, deleteSmsProducts, getSmsProducts, updateSmsProducts} from '@/api/sms'

const defaultProduct = {
  name: '',
  price: 0.00,
  msg_count: 0
}

export default {
  name: 'SmsProduct',
  data() {
    return {
      form:{
        product: Object.assign({}, defaultProduct),
        rules:{
          name: [
            { required: true, message: '请输入套餐名称', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '请输入套餐价格', trigger: 'blur' },
            { type: 'number', message: '套餐价格必须为数字值'}
          ],
          msg_count: [
            { required: true, message: '请输入短信条数', trigger: 'blur' },
            { type: 'number', message: '短信条数必须为数字值'}
          ]
        }
      },
      products: [],
      listQuery:{},
      dialogVisible: false,
      dialogType: 'new'
    }
  },
  created() {
    this.getProducts()
  },
  methods: {
    async getProducts() {
      const res = await getSmsProducts(this.listQuery)
      this.products = res.data.list
    },
    openCreateWindow() {
      this.dialogType = 'new'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.form.product = Object.assign({}, defaultProduct)
        this.$refs.smsProductForm.resetFields();
      })
    },
    openUpdateWindow(row) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs.smsProductForm.resetFields();
        this.form.product = Object.assign({}, row)
      })
    },
    handleCreate() {
      this.$refs.smsProductForm.validate((valid) => {
        if (valid) {
          createSmsProducts(this.form.product).then(resp => {
            if(resp.code === 20000) {
              this.dialogVisible = false
              this.$message.success('套餐已发布')
              this.getProducts()
            }
          })
        }
      })
    },
    handleEdit() {
      this.$refs.smsProductForm.validate((valid) => {
        if (valid) {
          updateSmsProducts(this.form.product).then(resp => {
            if(resp.code === 20000) {
              this.dialogVisible = false
              this.$message.success('套餐已修改')
              this.getProducts()
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否确认下架该套餐?', 'Warning', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteSmsProducts(row).then(res => {
            if(res.code === 20000) {
              this.$message({
                type: 'success',
                message: '已下架!'
              })
              this.getProducts()
            }
          })

        })
        .catch(err => { console.error(err) })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .roles-table {
    margin-top: 30px;
  }
  .permission-tree {
    margin-bottom: 30px;
  }
}
</style>
